package com.barradata.common.utils

import android.app.Activity
import android.graphics.Point
import android.view.View
import android.view.ViewGroup

/**
 * Created by WuNan-QF on 16/7/29.
 */
object ViewUtil {


    fun isVisible(view: View): Boolean {
        return view.visibility == View.VISIBLE
    }

    fun toggleVisibility(view: View) {
        view.visibility = if (isVisible(view)) View.GONE else View.VISIBLE
    }

    fun setVisible(view: View, visible: Boolean) {
        view.visibility = if (visible) View.VISIBLE else View.GONE
    }

    fun show(view: View) {
        setVisible(view, true)
    }

    fun hide(view: View) {
        setVisible(view, false)
    }


    fun getRootView(context: Activity): View {
        return (context.findViewById<View>(android.R.id.content) as ViewGroup).getChildAt(0)
    }

    fun getCenterX(view: View): Int {
        return (view.left + view.right) / 2
    }

    fun getCenterY(view: View): Int {
        return (view.top + view.bottom) / 2
    }

    fun getCenter(view: View): Point {
        return Point(getCenterX(view), getCenterY(view))
    }

    fun enable(view: View) {
        view.isEnabled = true
    }

    fun disable(view: View) {
        view.isEnabled = false
    }

    fun setEnable(view: View, enable: Boolean) {
        view.isEnabled = enable
    }
}
